package cn.itcast.nems.studentrecord.converter;

import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecord;
import cn.itcast.nems.studentrecord.dto.OrderPaidDTO;
import cn.itcast.nems.studentrecord.dto.StudentRecordPaidDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordJournalDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordFundFlowDTO;
import cn.itcast.nems.studentrecord.enumeration.FundFlowPaymentTypeEnum;

import java.time.LocalDateTime;

/**
 * @author liyong
 * create: 2023-11-20 15:07:43
 **/
public class StudentRecordFundFlowConverter {

    public static StudentRecordFundFlowDTO createInitStudentRecordFundFlow(final StudentRecordPaidDTO studentRecordPaidDTO,
                                                                       final StudentRecord studentRecord) {
        StudentRecordFundFlowDTO result = new StudentRecordFundFlowDTO();
        result.setStudentRecordId(studentRecord.getId());
        result.setBizDate(studentRecordPaidDTO.getBizDate());
        result.setReferenceType(ReferenceTypeEnum.INIT);
        result.setReferenceId(studentRecord.getId());
        result.setReferenceLineId(studentRecord.getId());
        result.setClazzId(studentRecord.getClazzId());
        result.setClazzName(studentRecord.getClazzName());
        // result.setPhaseNumber();
        result.setPaymentType(FundFlowPaymentTypeEnum.valueOf(studentRecordPaidDTO.getPaymentType()));
        result.setPaymentNo(studentRecordPaidDTO.getPaymentNo());
        result.setAmount(studentRecordPaidDTO.getAmount());
        // result.setCharge();
        result.setPaymentMethodCode(studentRecordPaidDTO.getPaymentMethodCode());
        result.setPaymentMethodName(studentRecordPaidDTO.getPaymentMethodName());
        result.setPaymentAccountCode(studentRecordPaidDTO.getPaymentAccountCode());
        result.setPaymentAccountName(studentRecordPaidDTO.getPaymentAccountName());
        result.setRemark("");
        return result;
    }

    public static StudentRecordFundFlowDTO createStudentRecordFundFlow(final StudentRecordPaidDTO studentRecordPaidDTO,
                                                                       final StudentRecord studentRecord, ReferenceTypeEnum referenceType) {
        StudentRecordFundFlowDTO result = new StudentRecordFundFlowDTO();
        result.setStudentRecordId(studentRecord.getId());
        result.setBizDate(studentRecordPaidDTO.getBizDate());
        result.setReferenceType(referenceType);
        result.setReferenceId(studentRecord.getId());
        result.setReferenceLineId(studentRecord.getId());
        result.setClazzId(studentRecord.getClazzId());
        result.setClazzName(studentRecord.getClazzName());
        // result.setPhaseNumber();
        result.setPaymentType(FundFlowPaymentTypeEnum.valueOf(studentRecordPaidDTO.getPaymentType()));
        result.setPaymentNo(studentRecordPaidDTO.getPaymentNo());
        result.setAmount(studentRecordPaidDTO.getAmount());
        // result.setCharge();
        result.setPaymentMethodCode(studentRecordPaidDTO.getPaymentMethodCode());
        result.setPaymentMethodName(studentRecordPaidDTO.getPaymentMethodName());
        result.setPaymentAccountCode(studentRecordPaidDTO.getPaymentAccountCode());
        result.setPaymentAccountName(studentRecordPaidDTO.getPaymentAccountName());
        result.setRemark("");
        return result;
    }

    public static StudentRecordFundFlowDTO createRefundTransferStudentRecordFundFlow(
            StudentRecord studentRecord, int amount, StudentRecordJournalDTO studentRecordJournal,
            LocalDateTime bizDate, String paymentAccountCode, String setPaymentAccountName,
            Integer multiPaymentAccountFlag, String originPaymentAccountCode, String originPaymentAccountName
    ) {
        StudentRecordFundFlowDTO result = new StudentRecordFundFlowDTO();
        result.setStudentRecordId(studentRecord.getId());
        result.setAmount(amount);
        /*
        LocalDateTime bizDate = amount > 0 ? studentRecordJournal.getTargetClazzInDate().atStartOfDay()
                : studentRecordJournal.getOriginClazzOutDate().atStartOfDay();
         */
        bizDate = bizDate == null ? LocalDateTime.now() : bizDate;
        result.setBizDate(bizDate);
        result.setReferenceType(studentRecordJournal.getJournalType());
        result.setReferenceId(studentRecordJournal.getJournalId());
        result.setReferenceLineId(studentRecordJournal.getJournalLineId());
        result.setClazzId(studentRecord.getClazzId());
        result.setClazzName(studentRecord.getClazzName());
        result.setPhaseNumber("1");
        result.setPaymentType(FundFlowPaymentTypeEnum.REFUND_TRANSFER);
        result.setPaymentNo(studentRecordJournal.getJournalLineId());
        result.setCharge(0);
        result.setPaymentMethodCode("");
        result.setPaymentMethodName("");
        result.setPaymentAccountCode(null == paymentAccountCode ? "" : paymentAccountCode);
        result.setPaymentAccountName(null == setPaymentAccountName ? "" : setPaymentAccountName);
        result.setRemark(FundFlowPaymentTypeEnum.REFUND_TRANSFER.getDesc());

        boolean multiPaymentAccount = false;
        if (multiPaymentAccountFlag != null && multiPaymentAccountFlag != 0) {
            multiPaymentAccount = true;
        }
        result.setMultiPaymentAccountFlag(multiPaymentAccount);
        result.setOriginPaymentAccountCode(originPaymentAccountCode);
        result.setOriginPaymentAccountName(originPaymentAccountName);
        return result;
    }

    public static StudentRecordFundFlowDTO createStudentRecordFundFlow(final StudentRecordPaidDTO studentRecordPaidDTO,
                                                                       final StudentRecordJournalDTO studentRecordJournal,
                                                                       final ReferenceTypeEnum referenceType) {
        StudentRecordFundFlowDTO result = new StudentRecordFundFlowDTO();
        result.setStudentRecordId(studentRecordJournal.getTargetStudentRecordId());
        result.setBizDate(studentRecordPaidDTO.getBizDate());
        result.setReferenceType(referenceType);
        result.setReferenceId(studentRecordJournal.getJournalId());
        result.setReferenceLineId(studentRecordJournal.getJournalLineId());
        result.setClazzId(studentRecordJournal.getTargetClazzId());
        result.setClazzName(studentRecordJournal.getTargetClazzName());
        // result.setPhaseNumber();
        result.setPaymentType(FundFlowPaymentTypeEnum.valueOf(studentRecordPaidDTO.getPaymentType()));
        result.setPaymentNo(studentRecordPaidDTO.getPaymentNo());
        result.setAmount(studentRecordPaidDTO.getAmount());
        // result.setCharge();
        result.setPaymentMethodCode(studentRecordPaidDTO.getPaymentMethodCode());
        result.setPaymentMethodName(studentRecordPaidDTO.getPaymentMethodName());
        result.setPaymentAccountCode(studentRecordPaidDTO.getPaymentAccountCode());
        result.setPaymentAccountName(studentRecordPaidDTO.getPaymentAccountName());
        result.setRemark("");
        return result;
    }

    public static StudentRecordFundFlowDTO createStudentRecordFundFlow(final StudentRecordPaidDTO studentRecordPaidDTO,
                                                                       StudentRecord studentRecord) {
        StudentRecordFundFlowDTO result = new StudentRecordFundFlowDTO();
        result.setStudentRecordId(studentRecord.getId());
        result.setClazzId(studentRecord.getClazzId());
        result.setClazzName(studentRecord.getClazzName());
        result.setBizDate(studentRecordPaidDTO.getBizDate());
        // result.setPhaseNumber();
        result.setPaymentType(FundFlowPaymentTypeEnum.valueOf(studentRecordPaidDTO.getPaymentType()));
        result.setPaymentNo(studentRecordPaidDTO.getPaymentNo());
        result.setAmount(studentRecordPaidDTO.getAmount());
        // result.setCharge();
        result.setPaymentMethodCode(studentRecordPaidDTO.getPaymentMethodCode());
        result.setPaymentMethodName(studentRecordPaidDTO.getPaymentMethodName());
        result.setPaymentAccountCode(studentRecordPaidDTO.getPaymentAccountCode());
        result.setPaymentAccountName(studentRecordPaidDTO.getPaymentAccountName());
        result.setRemark("");
        return result;
    }

    public static StudentRecordFundFlowDTO createStudentRecordFundFlow(final OrderPaidDTO orderPaid,
                                                                       StudentRecord studentRecord) {

        StudentRecordFundFlowDTO result = new StudentRecordFundFlowDTO();
        result.setStudentRecordId(studentRecord.getId());
        result.setClazzId(studentRecord.getClazzId());
        result.setClazzName(studentRecord.getClazzName());
        result.setReferenceType(ReferenceTypeEnum.ORDER);
        result.setBizDate(orderPaid.getBizDate());
        result.setReferenceId(orderPaid.getOrderId());
        final String orderLineId = orderPaid.getOrderLineIds().get(0);
        result.setReferenceLineId(orderLineId);
        // result.setPhaseNumber();
        result.setPaymentType(FundFlowPaymentTypeEnum.valueOf(orderPaid.getPaymentType()));
        result.setPaymentNo(orderPaid.getFlowNo());
        result.setAmount(orderPaid.getFlowAmount());
        result.setCharge(orderPaid.getCharge());
        result.setChargeRate(orderPaid.getChargeRate());
        result.setPaymentMethodCode(orderPaid.getPaymentMethodCode());
        result.setPaymentMethodName(orderPaid.getPaymentMethodName());
        result.setPaymentAccountCode(orderPaid.getPaymentAccountCode());
        result.setPaymentAccountName(orderPaid.getPaymentAccountName());
        result.setRemark(orderPaid.getRemark());
        cn.itcast.nems.order.dto.StudentRecordFundFlowDTO studentRecordFundFlowDTO = orderPaid.getStudentRecordFundFlowDTO();
        if (studentRecordFundFlowDTO != null) {
            result.setExtend1(studentRecordFundFlowDTO.getExtend1());
            result.setExtend2(studentRecordFundFlowDTO.getExtend2());
            result.setExtend3(studentRecordFundFlowDTO.getExtend3());
            result.setSchoolId(studentRecordFundFlowDTO.getSchoolId());
        }
        return result;
    }

}
